<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form ref="form" label-width="110px">
      <el-form-item label="预约时间选择：">
        <label>星期</label>
        <el-select
          v-model="selected.resTimeId"
          placeholder="请选择时间"
          style="width: 60px"
          @change="change_selected()"
        >
          <el-option
            v-for="item in reserveTimes"
            :key="item.resTimeId"
            :label="item.timeWeekday"
            :value="item.resTimeId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop label="开始时间">
        <el-input-number v-model="selected.timeStartTime" :min="0" />
      </el-form-item>
      <el-form-item label="结束时间">
        <el-input-number v-model="selected.timeEndTime" :min="0" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import integralGradeAPi from '@/api/core/integral-grade'
export default {
  data() {
    return {
      reserveTimes: {},
      selected: {},
      value: '',
      value_floor: '',
      // saveBtnDisabled: false, // 保存按钮是否禁用，防止表单重复提交
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    change_selected() {
      this.reserveTimes.forEach((element) => {
        if (element.resTimeId == this.selected.resTimeId) {
          this.selected.timeEndTime = element.timeEndTime
          this.selected.timeStartTime = element.timeStartTime
        }
      })
    },

    fetchData() {
      integralGradeAPi.listReserveTime().then((response) => {
        this.reserveTimes = response.data.list
      })
    },
    saveOrUpdate() {
      // 禁用保存按钮
      this.saveBtnDisabled = true
      this.saveData()
    },
    // 新增数据
    saveData() {
      console.log(this.selected)
      // debugger
      integralGradeAPi.saveReserveTime(this.selected).then((response) => {
        this.$message({
          type: 'success',
          message: response.message,
        })
        this.$router.push('/core/integral-grade/edit:reserveTime')
      })
    },
  },
}
</script>

<style scoped></style>
